<template>
  <ele-modal
    form
    :width="460"
    v-model="visible"
    :title="actionConfigs[action].title"
    @open="handleOpen"
    @close="handleCancel"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="currentRules"
      label-width="94px"
      @submit.prevent=""
    >
      <el-form-item
        label="字典数据名"
        prop="dict_data_name"
        v-if="showFields.includes('dict_data_name')"
      >
        <el-input
          clearable
          :maxlength="20"
          v-model="form.dict_data_name"
          :disabled="disabledFields.includes('dict_data_name')"
          placeholder="请输入字典数据名"
        />
      </el-form-item>

      <el-form-item
        label="字典数据值"
        prop="dict_data_code"
        v-if="showFields.includes('dict_data_code')"
      >
        <el-input
          clearable
          :maxlength="20"
          v-model="form.dict_data_code"
          :disabled="disabledFields.includes('dict_data_code')"
          placeholder="请输入字典数据值"
        />
      </el-form-item>

      <el-form-item
        label="排序号"
        prop="sort_number"
        v-if="showFields.includes('sort_number')"
      >
        <el-input-number
          :min="0"
          :max="9999"
          v-model="form.sort_number"
          :disabled="disabledFields.includes('sort_number')"
          placeholder="请输入排序号"
          controls-position="right"
          class="ele-fluid"
        />
      </el-form-item>

      <el-form-item label="备注" v-if="showFields.includes('comments')">
        <el-input
          :rows="4"
          :maxlength="200"
          v-model="form.comments"
          :disabled="disabledFields.includes('comments')"
          placeholder="请输入备注"
        />
      </el-form-item>

      <el-form-item
        label="审核意见"
        prop="audit_remark"
        v-if="action === 'audit'"
      >
        <el-input
          :rows="3"
          type="textarea"
          v-model="form.audit_remark"
          placeholder="请输入审核意见"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit">
        {{ actionConfigs[action].button }}
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { useFormEdit } from "@/hooks/useFormEdit";
import {
  addDictionaryData,
  updateDictionaryData,
  showDictionaryData,
  auditDictionaryData,
  pushDictionaryData,
} from "@/api/system/dictionary-data";

const props = defineProps({
  action: {
    type: String,
    default: "edit",
    validator: (value) =>
      ["add", "edit", "audit", "push", "view"].includes(value),
  },
  data: Object,
  dict_id: Number,
  modelValue: Boolean,
});

const emit = defineEmits(["update:modelValue", "done", "open"]);

const actionConfigs = {
  add: {
    title: "新增字典数据",
    button: "新增",
    api: addDictionaryData,
    showFields: ["dict_data_name", "dict_data_code", "sort_number", "comments"],
    disabledFields: [],
    rules: {
      dict_data_name: [
        { required: true, message: "请输入字典数据名", trigger: "blur" },
      ],
      dict_data_code: [
        { required: true, message: "请输入字典数据值", trigger: "blur" },
      ],
      sort_number: [
        { required: true, message: "请输入排序号", trigger: "blur" },
      ],
    },
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      formData.dict_id = props.dict_id;
      console.log("提交数据:", formData);
      // if (!formData.requiredField) {
      // return Promise.reject(new Error("缺少必填字段")); // 返回rejected Promise中断流程
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  edit: {
    title: "修改字典数据",
    button: "保存",
    api: updateDictionaryData, // 补充edit的API
    showFields: ["dict_data_name", "dict_data_code", "sort_number", "comments"],
    disabledFields: [],
    rules: {
      dict_data_name: [
        { required: true, message: "请输入字典数据名", trigger: "blur" },
      ],
      dict_data_code: [
        { required: true, message: "请输入字典数据值", trigger: "blur" },
      ],
      sort_number: [
        { required: true, message: "请输入排序号", trigger: "blur" },
      ],
    },
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      formData.dict_id = props.dict_id;
      console.log("提交数据:", formData);
      // // 添加实际验证逻辑
      // if (
      //   !formData.dict_data_name ||
      //   !formData.dict_data_code ||
      //   !formData.sort_number
      // ) {
      // return Promise.reject(new Error("请填写所有必填字段"));
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  audit: {
    title: "审核字典数据",
    button: "提交审核",
    api: auditDictionaryData, // 补充audit的API
    showFields: [
      "dict_data_name",
      "dict_data_code",
      "sort_number",
      "comments",
      "audit_remark",
    ],
    disabledFields: [
      "dict_data_name",
      "dict_data_code",
      "sort_number",
      "comments",
    ],
    rules: {
      audit_remark: [
        { required: true, message: "请输入审核意见", trigger: "blur" },
      ],
    },
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      formData.dict_id = props.dict_id;
      console.log("提交数据:", formData);
      // if (!formData.requiredField) {
      //   return Promise.reject(new Error("缺少必填字段")); // 返回rejected Promise中断流程
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  push: {
    title: "下推字典数据",
    button: "确认下推",
    api: pushDictionaryData, // 补充push的API
    showFields: ["dict_data_name", "dict_data_code", "sort_number"],
    disabledFields: ["dict_data_name", "dict_data_code", "sort_number"],
    rules: {},
    beforeSubmit: async ({ type, formData }) => {
      // 提交前的操作，可以修改表单数据
      formData.dict_id = props.dict_id;
      console.log("提交数据:", formData);
      // if (!formData.requiredField) {
      //   return Promise.reject(new Error("缺少必填字段")); // 返回rejected Promise中断流程
      // }
      return { type, formData }; // 必须返回处理后的表单数据
    },
  },
  view: {
    title: "查看字典数据",
    button: "确定",
    // api: showDictionaryData, // 补充view的API
    showFields: ["dict_data_name", "dict_data_code", "sort_number", "comments"],
    disabledFields: [
      "dict_data_name",
      "dict_data_code",
      "sort_number",
      "comments",
    ],
    rules: {},
  },
};

const {
  form,
  formRef,
  visible,
  loading,
  handleOpen,
  handleSubmit,
  showFields,
  disabledFields,
  currentRules,
  handleCancel,
} = useFormEdit(props, emit, {
  actionConfigs,
  formFields: {
    dict_id: props.dict_id, // 确保初始化时有值
    dict_data_id: void 0,
    dict_data_name: "",
    dict_data_code: "",
    sort_number: void 0,
    comments: "",
    audit_remark: "",
  },
  detailApi: showDictionaryData,
  idField: "dict_data_id",
});
</script>
